package at.fhooe.validation;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.validation.Check;

import at.fhooe.agaeScriptDsl.AgaeScriptDslPackage;
import at.fhooe.agaeScriptDsl.Button;
import at.fhooe.agaeScriptDsl.GuiItem;
import at.fhooe.agaeScriptDsl.HandlerItem;
import at.fhooe.agaeScriptDsl.Screen;
import at.fhooe.agaeScriptDsl.impl.BusinessLogicImpl;
import at.fhooe.agaeScriptDsl.impl.ButtonImpl;
import at.fhooe.agaeScriptDsl.impl.HandlerItemImpl;
import at.fhooe.agaeScriptDsl.impl.ScreenImpl;
 

public class AgaeScriptDslJavaValidator extends AbstractAgaeScriptDslJavaValidator {
	
	//Business logic validations
	
	@Check
	public void checkBLLayoutNameIsNotEmpty(BusinessLogicImpl bl){
		if(bl.getLayoutName().isEmpty()){
			error("Layout name cannot be empty.", AgaeScriptDslPackage.Literals.BUSINESS_LOGIC__LAYOUT_NAME);
		}
	}
	
	@Check
	public void checkBLHandlerItemIsNotEmpty(HandlerItemImpl item){
		if(item.getComponentFromName().isEmpty()){
			error("ComponentFrom name cannot be empty.", AgaeScriptDslPackage.Literals.HANDLER_ITEM__COMPONENT_FROM_NAME);
		}
		if(item.getComponentToName().isEmpty()){
			error("ComponentTo name cannot be empty.", AgaeScriptDslPackage.Literals.HANDLER_ITEM__COMPONENT_TO_NAME);
		}
	}
	
/*	@Check 
	public void checkLayoutNameIsTheSame(BusinessLogicImpl bl){
		TreeIterator<EObject> contents= bl.eAllContents();
        while(contents.hasNext()) {
            EObject next = contents.next();
            if (next instanceof Screen) {
            	if(bl.getLayoutName().equals(((Screen) next).getName())){
        			error("Layout name must be the same.", AgaeScriptDslPackage.Literals.BUSINESS_LOGIC__LAYOUT_NAME);
        		}
                return;
            }
        }
	}*/
	
	@Check
	public void checkBLStartsWithCapital(BusinessLogicImpl bl) {
		if (!Character.isUpperCase(bl.getName().charAt(0))) {
			warning("Name should start with uppercase letter.", AgaeScriptDslPackage.Literals.BUSINESS_LOGIC__NAME);
		}
	}
    	
	//Screen validations
	
	@Check
	public void checkScreenNameHasLowercaseLetters(ScreenImpl s) {
		int len = s.getName().length();
		for(int i=0; i<len; i++){
			if (Character.isUpperCase(s.getName().charAt(i))) {
				warning("Name should contain only lowercase letters.", AgaeScriptDslPackage.Literals.SCREEN__NAME);
			}
		}
	}
	
	@Check
	public void checkButtonTextIsNotEmpty(ButtonImpl btn) {	
		if (btn.getButtonText().isEmpty()) {
			error("Button text can't be empty.", AgaeScriptDslPackage.Literals.BUTTON__BUTTON_TEXT);
		}
	}
	
	@Check
	public void checkButtonHandlerNameIsNotEmpty(ButtonImpl btn) {
		if(btn.getBtnHandlerName().isEmpty()){
			error("BtnHandler name can't be empty.", AgaeScriptDslPackage.Literals.BUTTON__BTN_HANDLER_NAME);
		}
	}
	
	@Check 
	public void checkIfLayoutNameIsTheSame(ScreenImpl s){
		EObject obj = s.eContainer();
		TreeIterator<EObject> contents = obj.eAllContents();
		while(contents.hasNext()){
			EObject next = contents.next();
            if (next instanceof BusinessLogicImpl) {
            	if(!(((BusinessLogicImpl) next).getLayoutName().equals(s.getName()))){
            		error("Layout name must be the same.",AgaeScriptDslPackage.Literals.SCREEN__NAME);
            	}
            }
		}
	}
	
	
	

}
